﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include <QAction>
#include <QDebug>
#include "wizard.h"
#include "protree.h"
#include <QFileDialog>
#include "protreewidget.h"
#include "picshow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    this->setMinimumSize(1629,869);
    ui->setupUi(this);

    //创建文件菜单
    QMenu *menu_file = menuBar()->addMenu(tr(u8"文件(&F)"));
    //创建项目动作
    QAction * act_creat_pro = new QAction(QIcon(":/icon/createpro.png"),tr(u8"创建项目"),this);
    //设置快捷键
    act_creat_pro->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_N));
    menu_file->addAction(act_creat_pro);

    //打开项目动作
    QAction * act_open_pro = new QAction(QIcon(":/icon/openpro.png"),tr(u8"打开项目"),this);
    //设置快捷键
    act_open_pro->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
    menu_file->addAction(act_open_pro);

    //创建设置菜单
    QMenu * menu_set = menuBar()->addMenu(tr(u8"设置(&S)"));
    //设置背景音乐
    QAction * act_music = new QAction(QIcon(":/icon/music.png"), tr(u8"背景音乐"),this);
    act_music->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M));
    menu_set->addAction(act_music);


    //连接创建项目槽函数
    connect(act_creat_pro, &QAction::triggered, this, &MainWindow::SlotCreatePro);
    //连接打开项目的槽函数
    connect(act_open_pro, &QAction::triggered, this, &MainWindow::SlotOpenPro);

    //连接打开项目到目录树中
    _protree = new ProTree();
    ui->proLayout->addWidget(_protree);
    QTreeWidget* tree_widget = dynamic_cast<ProTree*>(_protree)->GetTreeWidget();
    auto * pro_tree_widget = dynamic_cast<ProTreeWidget*>(tree_widget);
    connect(this,&MainWindow::SigOpenPro,pro_tree_widget,&ProTreeWidget::SlotOPenPro);

    //右边
    _picshow = new PicShow();
    ui->picLayout->addWidget(_picshow);
    auto * pro_pic_show = dynamic_cast<PicShow*>(_picshow);
    //点击左侧目录树，显示右侧照片
    connect(pro_tree_widget,&ProTreeWidget::SigUpdateSelected,pro_pic_show,&PicShow::SlotSelectItem);
    connect(pro_pic_show,&PicShow::SigNextClicked,pro_tree_widget,&ProTreeWidget::SlotNextShow);
    connect(pro_pic_show,&PicShow::SigPreClicked,pro_tree_widget,&ProTreeWidget::SlotPreShow);
    //更新图片
    connect(pro_tree_widget,&ProTreeWidget::SigUpdatePic,pro_pic_show,&PicShow::SlotUpdatePic);

    connect(pro_tree_widget,&ProTreeWidget::SigClearSelected,pro_pic_show,&PicShow::SlotDeleteItem);

    connect(act_music,&QAction::triggered,pro_tree_widget,&ProTreeWidget::SlotSetMusic);
}

MainWindow::~MainWindow()
{
    delete ui;
}

//因为每次图像形状不同，都会导致重绘，这样会造成资源浪费，所以将重回事件写在MainWindow，只在MainWindow改变时重绘
void MainWindow::resizeEvent(QResizeEvent *event)
{
    auto * pro_pic_show = dynamic_cast<PicShow*>(_picshow);

    pro_pic_show->ReloadPic();
    QMainWindow::resizeEvent(event);
}

void MainWindow::SlotCreatePro(bool )
{
    qDebug() << "Slot Create Pro triggered\n";
    Wizard wizard(this);
    wizard.setWindowTitle(u8"创建项目");
    auto * page = wizard.page(0);
    page->setTitle(u8"设置项目配置");

    //连接信号和槽
    connect(&wizard,&Wizard::sigProSettings,dynamic_cast<ProTree*>(_protree),&ProTree::AddproToTree);

    wizard.show();
    wizard.exec();
    //断开所有信号todo....

}

void MainWindow::SlotOpenPro(bool )
{
    qDebug() << "Slot Open Pro triggered\n";

    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle(tr(u8"选择导入的文件夹"));
    file_dialog.setDirectory(QDir::currentPath());
    file_dialog.setViewMode(QFileDialog::Detail);

    QStringList fileNames;
    if(file_dialog.exec())
    {
        fileNames = file_dialog.selectedFiles();
    }
    if(fileNames.length() <= 0)
    {
        return;
    }

    QString import_path = fileNames.at(0);
    emit SigOpenPro(import_path);
}
